<div id="maincontent">
	<div id="content">
    <?php
    //   /?c=$1&a=$2&o=$3&x=$4&y=$5
    if (!isset($_GET['o']) || trim($_GET['o']) == "") {
      $place = "";
      $world = 1;
      $place_name = lico_lang('places', 'the.world');
      $place_name_desc = lico_lang('places', 'the.world.long');
      $code = "ALL";
      $place_longname = "";
      $hostcount = $nummachines;
      $users = $numusers;
      $population = $world_population;
    } else if (!isset($_GET['x']) || trim($_GET['x']) == "") {
      $place = strtoupper(trim($_GET['o']));
      $world = 0;
      $code = $place;
      $select = "SELECT * FROM `places` WHERE `code`='".lico_ESC($code)."' LIMIT 0,1";
	  $ret = $db->getAll_assoc($select);
      $f_key = intval($ret[0]['f_key']);
      $place_name = trim($ret[0]['name']);
      $place_longname = "(".trim($ret[0]['longname']).")";
      $place_name_desc = "";
      $hostcount = intval($ret[0]['hostcount']);
      $users = intval($ret[0]['users']);
      $population = intval($ret[0]['population']);
    } else {
      $place = strtoupper(trim($_GET['o']));
      $city = urldecode(trim($_GET['x']));
      $world = 0;
      $code = $place;
      $select = "SELECT * FROM `places` WHERE `code`='".lico_ESC($code)."' LIMIT 0,1";
	  $ret = $db->getAll_assoc($select);
      $f_key = intval($ret[0]['f_key']);
      $place_name = trim($ret[0]['name']);
      $place_longname = "(".trim($ret[0]['longname']).")";
      $place_name_desc = "";
      $hostcount = intval($ret[0]['hostcount']);
      $users = intval($ret[0]['users']);
      $population = intval($ret[0]['population']);
	}
    ?>
    <div class="box1">
	  <?php if (isset($_GET['x']) && trim($_GET['x']) != "") { ?>
		<h3><?php echo str_replace("#place#", $place_name." / ".$city, lico_lang('places', 'box1.page.title')); ?></h3>
	  <?php } else { ?>
		<h3><?php echo str_replace("#place#", $place_name, lico_lang('places', 'box1.page.title')); ?></h3>
	  <?php } ?>
      <?php echo $place_name_desc; ?><br/><br/>
	  <?php if (isset($_GET['x']) && trim($_GET['x']) != "") { ?>
			<b><a href="<?php echo $lico['baseurl']; ?>/places/stats/<?php echo $code; ?>.html">Back to <?php echo $place_name; ?>...</a></b><br/>
	  <?php } else if (isset($_GET['o']) && trim($_GET['o']) != "") { ?>
			<b><a href="<?php echo $lico['baseurl']; ?>/places/stats.html">Back to world...</a></b><br/>
	  <?php } ?>
      <b>Place name:</b>	<?php echo $place_name; ?> <?php echo $place_longname; ?><br/>
      <b>Place code:</b>	<?php echo $code; ?><br/>
      <b>Population:</b>	<?php echo number_format($population); ?><br/>
    </div>
    
    <?php
    $class = "odd";
    ?>
    <div class="box2">
      
 	  <?php if (isset($_GET['x']) && trim($_GET['x']) != "") { ?>
			<h3><?php echo lico_lang('places', 'box2.page.title.city'); ?></h3>
	  <?php } else if (isset($_GET['o']) && trim($_GET['o']) != "") { ?>
			<h3><?php echo lico_lang('places', 'box2.page.title'); ?></h3>
	  <?php } ?>
		
		<div class="description"><?php echo lico_lang('places', 'box2.page.description'); ?></div>
        <div class="infotable-line <?php echo $class; ?>">
          <ul class="infotable-ul">
            <li class="infotable-li col06p"><span><strong>Nr.</strong></span></li>
            <li class="infotable-li col40p"><span><strong>Name</strong></span></li>
			<?php if (!isset($_GET['o']) || trim($_GET['o']) == "") { ?>
				<li class="infotable-li col15p" style="text-align: right"><span><strong>Users</strong></span></li>
			<?php } else if (!isset($_GET['x']) || trim($_GET['x']) == "") { ?>
				<li class="infotable-li col15p" style="text-align: right"><span><strong>Users</strong></span></li>
			<?php } else { ?>
				<li class="infotable-li col15p" style="text-align: right"><span><strong>Registered machines</strong></span></li>
			<?php } ?>
			<?php if (!isset($_GET['o']) || trim($_GET['o']) == "") { ?>
				<li class="infotable-li col15p" style="text-align: right"><span><strong>Population</strong></span></li>
			<?php } ?>
          </ul>
        </div>
      <?php
	  if ((!isset($_GET['o']) || trim($_GET['o']) == "") && (!isset($_GET['x']) || trim($_GET['x']) == "")) {
		$sel = "SELECT c.f_key, c.code, c.name, c.longname, COUNT(u.f_key) AS users, c.population FROM places c LEFT JOIN persons p ON (p.country=c.code) LEFT JOIN users u ON (u.f_key=p.f_key) LEFT JOIN machines m ON (m.owner=u.f_key AND m.f_key IS NOT NULL) WHERE m.isactive='YES' AND m.f_key IS NOT NULL AND u.state=1 GROUP BY p.country ORDER BY c.name";
		$ret = $db->getAll_assoc($sel);
		for ($a=0; $a<count($ret); $a++) {
			if ($class == "odd") { $class = "even"; } else { $class = "odd"; }

			$f_key = intval($ret[$a]['f_key']);
			$name = trim($ret[$a]['name']);
			$code = strtolower(trim($ret[$a]['code']));
			$longname = trim($ret[$a]['longname']);
			$usercount = intval($ret[$a]['users']);
			$population = intval($ret[$a]['population']);
			?>
			<div class="infotable-line <?php echo $class; ?>">
			<ul class="infotable-ul">
				<li class="infotable-li col06p"><span><?php echo ($a+1); ?></span></li>
				<li class="infotable-li col40p"><span><a href="<?php echo $lico['baseurl']; ?>/places/stats/<?php echo $code; ?>.html"><span><?php echo htmlspecialchars($name); ?></span></a></span></li>
				<li class="infotable-li col15p" style="text-align: right"><span><?php echo number_format($usercount); ?></span></li>
				<li class="infotable-li col15p" style="text-align: right"><span><?php echo number_format($population); ?></span></li>
			</ul>
			</div>
			<?php
		}
	  } else if ((isset($_GET['o']) && trim($_GET['o']) != "") && (isset($_GET['x']) && trim($_GET['x']) != "")) {
		$sel = "SELECT * FROM `persons` p LEFT JOIN `users` u ON (p.f_key=u.f_key) WHERE p.country='".lico_ESC($code)."' AND `city`='".lico_ESC($city)."' AND u.state=1 ORDER BY p.name ASC";
		$ret = $db->getAll_assoc($sel);
		$linenum = 1;
		for ($a=0; $a<count($ret); $a++) {
			if ($class == "odd") { $class = "even"; } else { $class = "odd"; }
			
			$counternum = intval($ret[$a]['f_key']);
			$name = trim($ret[$a]['name']);
			$may_publish_name = trim($ret[$a]['may_publish']);
			$may_publish_email = trim($ret[$a]['privacy']);
			
			$select = "SELECT COUNT(m.f_key) AS num FROM `machines` m WHERE m.`owner`='".$counternum."' AND m.`isactive`='YES'";
			$selmach = $db->unbuffered_query_first($select);
			$num_active_machines = intval($selmach[0]);
			
			if ($num_active_machines >= 1) {
				if ($may_publish_name == "YES") {
					?>
					<div class="infotable-line <?php echo $class; ?>">
					<ul class="infotable-ul">
						<li class="infotable-li col06p"><span><?php echo $linenum; ?></span></li>
						<li class="infotable-li col40p"><span><a href="<?php echo $lico['baseurl']; ?>/user/<?php echo $counternum; ?>.html"><span><?php echo htmlspecialchars($name); ?></span></a></span></li>
						<li class="infotable-li col15p" style="text-align: right"><span><?php echo number_format($num_active_machines); ?></span></li>
					</ul>
					</div>
					<?php
					$linenum++;
				} else {
					?>
					<div class="infotable-line <?php echo $class; ?>">
					<ul class="infotable-ul">
						<li class="infotable-li col06p"><span><?php echo $linenum; ?></span></li>
						<li class="infotable-li col40p"><span>SECRET</span></li>
						<li class="infotable-li col15p" style="text-align: right"><span><?php echo number_format($num_active_machines); ?></span></li>
					</ul>
					</div>
					<?php
					$linenum++;
				}
				
			}
		}
	  } else {
		$sel = "SELECT p.city, COUNT(p.f_key) AS num FROM `persons` p LEFT JOIN `users` u ON (p.f_key=u.f_key) LEFT JOIN machines m ON (p.f_key=m.owner) WHERE m.isactive='YES' AND p.country='".lico_ESC($code)."' AND u.state=1 GROUP BY p.city ORDER BY num DESC";
		$ret = $db->getAll_assoc($sel);
		$linenum = 1;
		for ($a=0; $a<count($ret); $a++) {
			if ($class == "odd") { $class = "even"; } else { $class = "odd"; }
			
			$city = trim($ret[$a]['city']);
			$num = intval(trim($ret[$a]['num']));
			$city_url = urlencode($city);
			if ($city != "" && $num >= 1) {
				?>
				<div class="infotable-line <?php echo $class; ?>">
				<ul class="infotable-ul">
					<li class="infotable-li col06p"><span><?php echo $linenum; ?></span></li>
					<li class="infotable-li col40p"><span><a href="<?php echo $lico['baseurl']; ?>/places/stats/<?php echo $code; ?>/<?php echo $city_url; ?>.html"><span><?php echo htmlspecialchars($city); ?></span></a></span></li>
					<li class="infotable-li col15p" style="text-align: right"><span><?php echo number_format($num); ?></span></li>
				</ul>
				</div>
				<?php
				$linenum++;
			}
		}
	  }
      // $select = "SELECT COUNT(m.f_key) AS num FROM `machines` m LEFT JOIN `users` u ON (u.`f_key`=m.`owner`) WHERE u.`state`=1 AND m.`isactive`='YES'";
      ?>
    </div>
    
    
    
    
    
    
    
    
    
    
    
    
  </div>
</div>
